package com.example.supplychainbackend.entity.plan;

import com.example.supplychainbackend.controller.dto.plan.BOMInfoDto;
import com.example.supplychainbackend.controller.dto.plan.MatchingPlanDto;
import com.example.supplychainbackend.controller.dto.plan.cost.CostInfoDto;
import com.example.supplychainbackend.entity.plan.cost.CostInfoEntity;
import com.example.supplychainbackend.tool.DateTimeUtils;
import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.CreationTimestamp;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;


@Entity
@Table(name = "plan_order")
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class PlanOrderEntity {
    @Id
    @Column(name = "work_order_number")
    String workOrderNumber;

    String salesOrderNo;

    String productCode;
    String productName;
    String productionProgress;
//    String plannedStartDate;
//    String plannedEndDate;
//    String actualStartDate;
//    String actualEndDate;

//    String contractName;
//    String customerName;
//    String contractNo;
//    String deliveryDate;
//    String salesOrderName;

    Integer workOrderStatus;
    String completionStatus;

    Integer storageStatus;

//    Integer storageQuantity;
    Integer workOrderType;

    Integer productionStatus;
    Integer reportStatus;
    String cabinetNo;

//    String deliveryGroupNo;

    String sourceMaterialNo;
    String sourceMaterialDesc;
    String sourceDocNo;
    String sourceLineNo;
    String creator;

    @CreationTimestamp
    String createDate;

//    String processRoute;
//    String area;
//    String productionLine;

    String completionNote;

    @Embedded
    CostInfoEntity cost;

    @OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "po_number")
    ProductionScheduleEntity productionSchedule;

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "po_number")
    List<BOMInfoEntity> bomItems;

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "po_number")
    List<MaterialInfoEntity> materialItems;

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "po_number")
    List<MatchingPlanEntity> matchingPlans;

    String	userName;
    String	enterpriseName;
    Boolean	onChain;
    String	transactionId;
    String	commitTime;
    String	userId;
    String	enterpriseId;

    @Transient
    private static final AtomicInteger counter = new AtomicInteger(1);

    @PrePersist
    public void generateId() {
        if (workOrderNumber == null || workOrderNumber.equals("")) {
            workOrderNumber = String.format("%s%s%04d",
                    "MO", DateTimeUtils.getCurrentDateForID(), counter.getAndIncrement());
        }
    }
}
